<?php
//  读取 php 原始数组 配置- 数组对象处理
function config($type='objct')
{
    $file_dir = str_replace('\\','/',dirname(__FILE__));
    $dir = str_replace('/common','',$file_dir);
    $file = $file_dir.'/config.php';
    if(is_file($file)){
        require($file);
        //require_once($file); - 造成config函数不可重复引入文件
        if($type != 'objct') return $config;
        if(!empty($config)){
            $obj = (object)$config;
            return $obj;
        }
    }
    return null;
}
function __autoload($name)
{   
    $c = config();
    $dir = $c->dir;
    $file = $dir.'/common/'.$name.'.php';
    $file = is_file($file)? $file : $dir.'/common/class/'.$name.'.php';
    if(is_file($file)) require_once($file);
}
//  读取 ini配置文件
function C($name=null)
{
    $file = dirname(__FILE__).'/config.ini';
    if(is_file($file)){
        $arr = parse_ini_file($file);
        if(empty($name)) return $arr;
        if(array_key_exists($name,$arr)) return $arr[$name];
    }
    return '';
}

// url 解析- 
function addr5()
{
    $addr = $_SERVER['REQUEST_URI'];
    $c = config();
    $urlRule = $c->url_rewite;
    if($urlRule == 'HTML'){//   伪静态
        fakeHtml($c,$addr);
        return true;
    }
    $prjname = $c->prjname;
    $dir = $c->dir;
    if($prjname) $addr = str_replace('/'.$prjname,'',$addr);//  去掉项目前缀    
    if($addr == '/') $addr = null;//debug($addr);
    if((is_dir($dir.$addr) || is_file($dir.$addr)) && !empty($addr)) return true;//   有实体时跳过URL重写
    $err = str_replace('.html','',$c->error);
    if(substr_count($addr,'.php')>0){              
        header('location:'.$err);
    }    
    $tmp = explode('?',$addr);
    $addr = $tmp[0];
    $tmp = explode('/',$addr);
    $app = array_pop($tmp);
    if(count($tmp) == 2){//  3级action支持
        $clsName = $tmp[1].'Class';
        $clsFile = $dir.'/www/'.$clsName.'.php';
        if(is_file($clsFile)){//    action子类求其
            $app = str_replace('.php','',$app);
            require_once($clsFile);
            $_POST['class_action'] = $app;
            eval('$page = new '.$clsName.'();');
            //return true;
            die;
        }
    }
    $ckdir = $dir.implode('/',$tmp);
    if(!is_dir($ckdir)) header('location:'.$err);
    //  系统自动化API
    $app = str_replace('.php','',$app);
    if($app == 'auto'){
        require_once($dir.'/common/autoClass.php');
        return true;
    }
    //  系统常规
    $name = $app.'Class';
    $file = $dir.'/www/'.$name.'.php';
    require_once($file);
    eval('$page = new '.$name.'();'); 
    die;
}
//  伪静态处理- 示例index.html
function fakeHtml($c,$addr)
{
    $prjname = $c->prjname;
    $dir = $c->dir;
    if($prjname) $addr = str_replace('/'.$prjname,'',$addr);//  去掉项目前缀    
    if($addr == '/') $addr = null;//debug($addr);
    if((is_dir($dir.$addr) || is_file($dir.$addr)) && !empty($addr)) return true;//   有实体时跳过URL重写
    $err = $c->error;
    if(substr_count($addr,'.html') == 0){                
        header('location:'.$err);
    }    
    $tmp = explode('?',$addr);
    $addr = $tmp[0];
    $tmp = explode('/',$addr);
    $app = array_pop($tmp);
    if(count($tmp) == 2){//  3级action支持
        $clsName = $tmp[1].'Class';
        $clsFile = $dir.'/www/'.$clsName.'.php';
        if(is_file($clsFile)){//    action子类求其
            $app = str_replace('.html','',$app);
            require_once($clsFile);
            $_POST['class_action'] = $app;
            eval('$page = new '.$clsName.'();');
            return true;
        }
    }
    $ckdir = $dir.implode('/',$tmp);
    if(!is_dir($ckdir)) header('location:'.$err);
    //  系统自动化API
    if($app == 'auto.html'){
        require_once($dir.'/common/autoClass.php');
        return true;
    }
    //  系统常规
    $name = str_replace('.html','',$app).'Class';
    $file = $dir.'/www/'.$name.'.php';
    require_once($file);
    eval('$page = new '.$name.'();'); 
}
//  与 addr 分离
function parseAddr()
{
    $addr = $_SERVER['REQUEST_URI']; 
    $tmp = explode('?',$addr);
    $addr = $tmp[0];
    $tmp = explode('/',$addr);
    $name = array_pop($tmp);
    $app = str_replace('.html','',$name);
    return [
        'app'  => $app,
        'name' => $name
    ];
}
// 生成调转URL
function forward($name,$c=null)
{
    if(!is_object($c)) $c = config();
    $url = '/';
    $url .= $c->prjname;
    $url = $name.($c->url_rewite== 'HTML'? '.html':'');
    return $url;
}
//  目录复制
function copydir($src,$des)
{
    //$src = rtrim(rtrim($src,'/'),'\\').'/';// 无效
    $src .= '/';
    $des .= '/';
    if(is_dir($src)){
        if(!is_dir($des)) mkdir($des);
        $data = scandir($src);
        foreach($data as $v){
            if(in_array($v,array('.','..'))) continue;
            if(filetype($src.$v) == 'file'){
                copy($src.$v,$des.$v);
            }elseif(filetype($src.$v) == 'dir'){
                copydir($src.$v,$des.$v);
            }

        }
    }else echo '<h4>'.$src.'目录不存在！</h4>';
}
function debug($data,$dis=null)
{
    $c = config();
	$dir = $c->debug;
	if(!is_dir($dir)) mkdir($dir);
	$log = "\r\n快速调试变量(Common/Function)：".date('Y-m-d H:i:s').":::\r\n";
	//由于object无法使用print_r($object,true)
	if(is_object($data) || $dis){
		//$fname = $dir.date('Y-m-d').'@'.rand(1000,9999).'.debug';
		//file_put_contents($fname,$data);//可写入maxed类型
		if($dis) echo '<p>对象可能会解析失败，而且用显示！</p><br>'.$log;
		echo '<pre>';print_r($data);
		return false;
	}
	$log .= is_array($data)? print_r($data,true):$data;
	$fname = $dir.date('Y-m-d').'.debug';
	if(is_file($fname)){
		$fh = fopen($fname,'a+');
		fwrite($fh,$log);//只能写入string
		fclose($fh);
	}else{
		file_put_contents($fname,$log);//可写入maxed类型
	}
}

//动态获取相对页面 $opt R/r 返回廋URL; array 重置url - PHP版本不可兼容
function url($opt=null,$append=false)
{
	$url = $_SERVER["REQUEST_URI"];//胖URL
	$param = $_SERVER["QUERY_STRING"];//URL参数
    $url = str_replace('?'.$param,'',$url);
	$data = $_GET;
    if($append) $data = array_merge($data,$opt);//追加
    else $data = $opt;    
    //print_r($data);
    $tmp = array();
    foreach($data as $k=>$v){
        $tmp[] = $k.'='.$v;
    }
    if(count($tmp)>0) $url .= '?'.implode('&',$tmp);
    //echo'<h4>'.$url.'</h4>';
	return $url;
}